<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'sports:sportsClassTeacerRelation:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsClassTeacerRelationForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { SportsClassTeacerRelation, sportsClassTeacerRelationSave, sportsClassTeacerRelationForm } from '/@/api/sports/sportsClassTeacerRelation';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('sports.sportsClassTeacerRelation');
  const { showMessage } = useMessage();
  const record = ref<SportsClassTeacerRelation>({} as SportsClassTeacerRelation);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增老师&班级对应关系') : t('编辑老师&班级对应关系'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('年级'),
      field: 'gradeName',
      component: 'Select',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
      },
    },
    {
      label: t('班级'),
      field: 'className',
      component: 'Select',
      componentProps: {
        dictType: 'vke_class',
        allowClear: true,
      },
    },
    {
      label: t('体育老师'),
      field: 'sportsTeacher',
      component: 'Select',
      componentProps: {
        dictType: '',
        allowClear: true,
        mode: 'multiple',
      },
    },
    {
      label: t('医务老师'),
      field: 'medicalTeacher',
      component: 'Select',
      componentProps: {
        dictType: '',
        allowClear: true,
        mode: 'multiple',
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ loading: true });
    const res = await sportsClassTeacerRelationForm(data);
    record.value = (res.sportsClassTeacerRelation || {}) as SportsClassTeacerRelation;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        relationId: record.value.relationId,
      };
      // console.log('submit', params, data, record);
      const res = await sportsClassTeacerRelationSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
